<?php
global $_W,$_GPC;
$op = empty($_GPC['op'])?'list':$_GPC['op'];
if($op == 'info'){
    $type = $_GPC['type'];
    $position = $_GPC['position'];
    $pindex = max(1,intval($_GPC['page']));
    $psize = 5;
    $list = pdo_fetchall("SELECT * FROM ".tablename('goouc_class_poster')." WHERE weid=:weid AND `type`=:type AND `position`=:position order by position LIMIT ".($pindex - 1)*$psize.','.$psize,array(':weid'=>$_W['uniacid'],':type'=>$type,':position'=>$position));

    foreach($list as $k=>$v){
        if($v['position'] == 2){
            if($v['levelid'] == 0){
                $list[$k]['levelname'] = '学员';
            }else{
                $list[$k]['levelname'] = pdo_get('goouc_class_level',array('weid'=>$_W['uniacid'],'id'=>$v['levelid']))['name'];
            }
        }
    }
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('goouc_class_poster')." WHERE weid=:weid AND `type`=:type",array(':weid'=>$_W['uniacid'],':type'=>$type));
    $page = pagination($total,$pindex,$psize);
    $num = 1+($pindex-1)*$psize;
}elseif($op == 'post'){
    $position = $_GPC['position'];
    $type = $_GPC['type'];
    $id = $_GPC['id'];
    if($position == 2){
        $levellist = pdo_getall('goouc_class_level',array('weid'=>$_W['uniacid']));
    }
    $info = pdo_get('goouc_class_poster',array('weid'=>$_W['uniacid'],'id'=>$id));
    if($info['position'] == 2){
        $levellist = pdo_getall('goouc_class_level',array('weid'=>$_W['uniacid']));
    }
    if(checksubmit('submit')){
        $data['weid'] = $_W['uniacid'];
        $data['title'] = $_GPC['title'];
        $data['image'] = $_GPC['image'];
        $data['order'] = $_GPC['order'];
        $data['status'] = $_GPC['status'];
        $data['position'] = $_GPC['position'];
        $data['type'] = $_GPC['type'];
        $data['createtime'] = time();
        if($data['position'] == 2){
            $data['levelid'] = $_GPC['levelid'];
        }
        if(empty($data['title']) || empty($data['image'])){
            message('标题或图片不能为空','','error');
        }
        if ($this->userid == $_W['uid']) {
            message($this->msg);
        }
        if(empty($id)){
            $res = pdo_insert('goouc_class_poster',$data);
            if($res){
                $arr = array(
                    'weid'=>$_W['uniacid'],
                    'admin_uid'=>$_W['uid'],
                    'admin_username'=>$_W['username'],
                    'log_type'=>1,
                    'function'=>'海报设置->海报设置',
                    'content'=>'新增海报背景'
                );
                $this->addlog($arr);
            }
        }else{
            $res = pdo_update('goouc_class_poster',$data,array('id'=>$id));
            if($res){
                $arr = array(
                    'weid'=>$_W['uniacid'],
                    'admin_uid'=>$_W['uid'],
                    'admin_username'=>$_W['username'],
                    'log_type'=>3,
                    'function'=>'海报设置->海报设置',
                    'content'=>'更改海报信息'
                );
                $this->addlog($arr);
            }
        }
        if($res){
            message('操作成功',$this->createWebUrl('poster'),'success');
        }else{
            message('操作失败',$this->createWebUrl('poster'),'error');
        }

    }
}elseif($op == 'delete'){
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    $id = $_GPC['id'];
    $res = pdo_delete('goouc_class_poster',array('id'=>$id));
    if($res){
        message('删除成功',$this->createWebUrl('card'),'success');
    }else{
        message('删除失败',$this->createWebUrl('card'),'error');
    }
}elseif($op == 'changestatus'){
    if ($this->userid == $_W['uid']) {
        $message = array('success'=>0,'mess'=>$this->msg);
        echo json_encode($message);exit;
    }
    $id = $_GPC['id'];
    $data['status'] = $_GPC['sta'];
    $info = pdo_get('goouc_class_poster',array('id'=>$id,'weid'=>$_W['uniacid']));
    if($data['status'] == $info['status']){
        $message = array('success'=>0,'修改失败，请刷新页面后重新修改');
    }else{
        $res = pdo_update('goouc_class_poster',$data,array('id'=>$id,'weid'=>$_W['uniacid']));
        if($res){
            $message = array('success'=>1,'mess'=>'修改成功');
        }else{
            $message = array('success'=>0,'mess'=>'修改失败');
        }
    }
    echo json_encode($message);exit;
}elseif($op == 'list'){
    $list1 = pdo_getall('goouc_class_type_status',array('weid'=>$_W['uniacid'],'position'=>1));
    $list2 = pdo_getall('goouc_class_type_status',array('weid'=>$_W['uniacid'],'position'=>2));
}elseif($op == 's2'){
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    for($i = 0;$i <= 5;$i++ ){
        $data2 = array('weid'=>$_W['uniacid'],'name'=>'模板'.($i+1),'type'=>($i+1),'status'=>0,'position'=>2,'image'=>'images/signin_qrcode/'.($i+1).'.jpg');
        pdo_insert('goouc_class_type_status',$data2);
    }
    message('生成成功',$this->createWebUrl('poster'),'success');
}elseif($op == 's1'){
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    for($i = 0;$i <= 5;$i++ ){
        $data1 = array('weid'=>$_W['uniacid'],'name'=>'模板'.($i+1),'type'=>($i+1),'status'=>0,'position'=>1,'image'=>'images/lesson_qrcode/'.($i+1).'.jpg');
        pdo_insert('goouc_class_type_status',$data1);
    }
    message('生成成功',$this->createWebUrl('poster'),'success');
}elseif($op == 'type_status'){
    if ($this->userid == $_W['uid']) {
        $message = array('success'=>0,'mess'=>$this->msg);
        echo json_encode($message);exit;
    }
    $id = $_GPC['id'];
    $data['status'] = $_GPC['sta'];
    $info = pdo_get('goouc_class_type_status',array('id'=>$id,'weid'=>$_W['uniacid']));
    if($data['status'] == $info['status']){
        $message = array('success'=>0,'修改失败，请刷新页面后重新修改');
    }else{
        $res = pdo_update('goouc_class_type_status',$data,array('id'=>$id,'weid'=>$_W['uniacid']));
        if($res){
            $message = array('success'=>1,'mess'=>'修改成功');
        }else{
            $message = array('success'=>0,'mess'=>'修改失败');
        }
    }
    echo json_encode($message);exit;
}
include $this->template('web/poster');
?>